package org.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.example.common.R;
import org.example.pojo.entity.SysRole;
import org.example.pojo.req.RoleMenuReq;
import org.example.pojo.vo.RoleVo;
import org.example.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@Api(tags = "后台用户角色接口")
@RestController
@RequestMapping("/api/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 获取角色列表数据
     *
     * @param page
     * @param size
     * @param roleName
     * @param roleKey
     * @param status
     * @param dateTime
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "page", value = ""),
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "size", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "roleName", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "perStr", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "status", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "dateTime", value = "")
    })
    @ApiOperation(value = "获取角色列表数据", notes = "获取角色列表数据", httpMethod = "GET")
    @GetMapping("/list/{page}/{size}")
    public R getListRole(@PathVariable(required = false) Integer page,
                         @PathVariable(required = false) Integer size,
                         @RequestParam(required = false) String roleName,
                         @RequestParam(required = false) String roleKey,
                         @RequestParam(required = false) Integer status,
                         @RequestParam(required = false) String dateTime) {
        Page<RoleVo> sysUserPage = sysRoleService.pageList(page, size, roleName, roleKey, status, dateTime);
        return R.ok(sysUserPage);
    }


    /**
     * 添加角色信息
     *
     * @param roleMenuReq
     * @return
     */
    @ApiOperation(value = "添加角色", notes = "添加角色", httpMethod = "POST")
    @PostMapping("/add")
    public R addRole(@RequestBody RoleMenuReq roleMenuReq) {
        sysRoleService.saveRole(roleMenuReq);
        return R.ok("添加成功");
    }

    /**
     * 修改角色菜单信息
     *
     * @param roleMenuReq
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "RoleMenuReq", name = "roleMenuReq", value = "", required = true)
    })
    @ApiOperation(value = "修改角色菜单信息", notes = "修改角色菜单信息", httpMethod = "PUT")
    @PutMapping("/edit")
    public R editRoleMenu(@RequestBody RoleMenuReq roleMenuReq) {
        sysRoleService.editRoleMenu(roleMenuReq);
        return R.ok(null);
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "roleId", value = "", required = true)
    })
    @ApiOperation(value = "删除角色", notes = "删除角色", httpMethod = "DELETE")
    @DeleteMapping("remove/{roleId}")
    public R remove(@PathVariable String roleId) {
        sysRoleService.removeRole(roleId);
        return R.ok(null);
    }
}
